<?php
/**
 * Created by PhpStorm.
 * User: sojo
 * Date: 2018/1/3
 * Time: 11:40
 */

namespace App\Http\Admin\Administration\Models;

use App\Eloquent\Oa\FlowConfigProcess;
use App\Repositories\OA\FlowConfigRepository;
use App\Repositories\OA\FlowRepository;
use Framework\BaseClass\Http\Admin\Model;

class FlowConfig extends Model
{
    public function getFlowList($companyId, $page, $pageSize)
    {
        $flow = new FlowConfigRepository();
        $flowList = $flow->getPagingList($page, $pageSize, [
            'oa_company_id' => $companyId
        ]);

        return $flowList;
    }

    public function getFlowInfo($id)
    {
        $flow = new FlowRepository();
        $flowInfo = $flow->find([
            'id' => $id
        ]);

        $flowInfo->display = $flowInfo->display ? true : false;

        return $flowInfo;
    }

    public function updateFlowInfo($id, $attributes)
    {
        $flow = new FlowRepository();
        $flow->update($id, $attributes);
    }

    /**
     * 获取流程配置步骤列表
     * @author wenwenbin
     * @param int $flowConfigId 流程配置id
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getFlowConfigList($flowConfigId)
    {
        $flowConfigList = FlowConfigProcess::where([['oa_flow_config_id', $flowConfigId], ['display', 1]])
            ->with('contactsInfo')
            ->get();
        foreach ($flowConfigList as $flowConfig) {
            $flowConfig->operator = is_null($flowConfig->contactsInfo) ? '' : $flowConfig->contactsInfo->name;
            unset($flowConfig->contactsInfo);
        }
        return $flowConfigList;
    }

    /**
     * 添加流程配置步骤
     * @author wenwenbin
     * @param int $pid 父id
     * @param int $oa_flow_config_id 流程配置id
     * @param int $operator_id 操作人员id
     * @param string $description 步骤名称
     * @return bool
     */
    public function addFlowConfigProcess($pid, $oa_flow_config_id, $operator_id, $description)
    {
        $flowConfigProcess = new FlowConfigProcess();
        \DB::beginTransaction();
        $flowConfigProcess->fill([
            'pid'               => $pid,
            'oa_flow_config_id' => $oa_flow_config_id,
            'operator_id'       => $operator_id,
            'description'       => $description
        ]);
        $result = $flowConfigProcess->save();
        $flowConfigProcessSon = FlowConfigProcess::where([['id', '<>', $flowConfigProcess->id], ['pid', $pid],
            ['oa_flow_config_id', $oa_flow_config_id]])
            ->get();
        foreach ($flowConfigProcessSon as $item) {
            $item->pid = $flowConfigProcess->id;
            $result = $item->save() && $result;
        }
        if ($result) {
            \DB::commit();
            return $flowConfigProcess->id;
        } else {
            \DB::rollBack();
            return false;
        }
    }

    /**
     * 获取流程配置步骤id
     * @author wenwenbin
     * @param int $id 流程配置步骤id
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[]
     */
    public function getFlowConfigProcessInfo($id)
    {
        $flowConfigProcess = FlowConfigProcess::with('contactsInfo')->find($id, ['id', 'operator_id', 'description','oa_flow_config_id']);
        if (is_null($flowConfigProcess)) xThrow(ERR_OA_CONTACTS_NOT_EXIST);
        $flowConfigProcess->operator_name = $flowConfigProcess->contactsInfo ? $flowConfigProcess->contactsInfo->name : '';

        //易管通用户信息获取方式不同
        $userInfo = \App\Eloquent\Ygt\DepartmentUser::getCurrentInfo($flowConfigProcess->operator_id)->toArray();
        $flowConfigProcess->operator_name = $userInfo['truename'];

        return $flowConfigProcess;
    }


    /**
     * 编辑流程配置步骤
     * @author wenwenbin
     * @param int $id 流程配置步骤id
     * @param int $operator_id 操作人员id
     * @param string $description 步骤名称
     * @return bool
     */
    public function editFlowConfigProcess($id, $operator_id, $description)
    {
        $flowConfigProcess = FlowConfigProcess::find($id);
        if (is_null($flowConfigProcess)) xThrow();
        $flowConfigProcess->fill([
            'operator_id'       => $operator_id,
            'description'       => $description
        ]);
        return $flowConfigProcess->save();
    }

    /**
     * 删除流程配置步骤
     * @author wenwenbin
     * @param int $id 流程配置步骤id
     * @return bool
     * @throws \Exception
     */
    public function deleteFlowConfigProcess($id)
    {
        $flowConfigProcess = FlowConfigProcess::find($id);
        if (is_null($flowConfigProcess)) xThrow();
        $flowConfigProcessSon = FlowConfigProcess::where('pid', $flowConfigProcess->id)->get();
        $result = true;
        foreach ($flowConfigProcessSon as $item) {
            $item->pid = $flowConfigProcess->pid;
            $result = $item->save() && $result;
        }
        $result = $flowConfigProcess->delete() && $result;
        if ($result) {
            \DB::commit();
            return true;
        } else {
            \DB::rollBack();
            return false;
        }
    }
}